<?php
$op = get_get("op");
if ($op == "") {
    $op = "requestPage";
}
if ($op == "requestPage") {
    $page = get_get("page");
    if (! $page)
        $page = "apply_class";
    if ($page == "add_class") {
        loadTemp("admin/models/class/add_class.php");
        die();
    }
    if ($page == "edit_class") {
        loadTemp("admin/models/class/edit_class.php");
        die();
    }
    if ($page == "teacher_class") {
        loadTemp("admin/models/class/teacher_class.php");
        die();
    }
    if ($page == "apply_class") {
        loadTemp("admin/models/class/apply_class.php");
        die();
    }
    if ($page == "list_class") {
        loadTemp("admin/models/class/list_class.php");
        die();
    }
    if ($page == "manage_class") {
        loadTemp("admin/models/class/manage_class.php");
        die();
    }
}

if ($op == "add_class") {
    checkAdminPermission("admin.class.add_class", "admin.class.*", true); // 权限判断
    $classid = get_post("classid");
    if (! $classid)
        returnMessage(- 1, "您未填写班级号");
    if (! isNumber($classid))
        returnMessage(- 2, "班级账号必须为纯数字");
    if (getClass($classid))
        returnMessage(- 2, "班级号不能重复，$classid 已经存在");
    $displayname = get_post("displayname");
    if (! $displayname)
        returnMessage(- 3, "您未填写班级名称");
    $introduce = get_post("introduce");
    if (! $introduce)
        returnMessage(- 4, "您未填写班级介绍");
    $admission_year = get_post("admission_year");
    if (! $admission_year)
        returnMessage(- 4, "您未填写入学年份");
    if (! isNumber($admission_year))
        returnMessage(- 4, "无效的入学年份，只能是数字");
    $master = get_post("master");

    addClass($classid, $displayname, $introduce, $master, $admission_year);
    returnMessage(0, "添加成功，您的操作请求已受理", array(
        "op" => "reset"
    ));
}
if ($op == "search_master") {
    checkAdminPermission("admin.class.add_class", "admin.class.*", true); // 权限判断
    $account = get_post("account");
    if (! $account)
        returnMessage(- 1, "你未填写账号");
    $uid = getUID($account);
    if ($uid == - 1)
        returnMessage(- 1, "无法查询到你所寻找的用户");
    $profile = getProfile($uid);
    if (! $profile)
        returnMessage(- 2, "无法查询到此人的档案");
    if ($profile["user_type"] != 2)
        returnMessage(- 3, "你所要添加的用户，不是一个教师，如需指定他为班主任，请通过成员编辑选项卡，将其编辑为教师后再进行次操作");
    $class = getMasterClass($uid)["classid"];
    if ($class) {
        $classdata = getClass($class);
        if ($classdata)
            returnMessage(- 3, "你所要添加的用户，正在管理" . $classdata["displayname"] . "无法同时兼任其他班级班主任");
    }
    $back = array();
    $back["op"] = "master_search";
    $back["card"] = getCardInfo($uid);
    returnMessage(3, "搜索成功", $back);
}
if ($op == "search_edit_class") {
    checkAdminPermission("admin.class.edit_class", "admin.class.*", true); // 权限判断
    $classid = get_post("classid");
    if (! $classid)
        returnMessage(- 1, "你没有输入班级");
    $class = getClass($classid);
    if (! $class)
        returnMessage(- 2, "指定班级未找到");
    $back = array();
    $back["class"] = $class;
    $back["card"] = getCardInfo($class["master"]);
    $back["op"] = "class_search";
    returnMessage(3, "搜索成功", $back);
}
if ($op == "edit_class") {
    checkAdminPermission("admin.class.edit_class", "admin.class.*", true); // 权限判断
    $classid = get_post("classid");
    if (! $classid)
        returnMessage(- 1, "你没有输入班级");
    $class = getClass($classid);
    if (! $class)
        returnMessage(- 2, "指定班级未找到");
    $displayname = get_post("displayname");
    if (! $displayname)
        returnMessage(- 3, "您未填写班级名称");
    $introduce = get_post("introduce");
    if (! $introduce)
        returnMessage(- 4, "您未填写班级介绍");
    $admission_year = get_post("admission_year");
    if (! $admission_year)
        returnMessage(- 4, "您未填写入学年份");
    $enable = get_post("enable");
    $master = get_post("master");
    updateClassData($classid, $displayname, $introduce, $master, $admission_year, $enable);
    returnMessage(0, "编辑成功", array(
        "op" => "reset"
    ));
}
if ($op == "search_teacher_class") {
    checkAdminPermission("admin.class.teacher_class", "admin.class.*", true); // 权限判断
    $classid = get_post("classid");
    if (! $classid)
        returnMessage(- 1, "你没有输入班级");
    $class = getClass($classid);
    if (! $class)
        returnMessage(- 2, "指定班级未找到");
    $back = array();
    $teachers = getTeachersByClassid($classid);
    if (! $teachers) {
        returnMessage(- 3, "你所进入的班级没有任课老师，请添加", array(
            "op" => "showTeacherSelecter"
        ));
    }
    $back["teacher"] = $teachers;
    $back["classdata"] = $class;
    $back["op"] = "search_teacher_class";
    returnMessage(3, "搜索成功", $back);
}
if ($op == "remove_teacher") {
    checkAdminPermission("admin.class.teacher_class", "admin.class.*", true); // 权限判断
    $uid = get_post("uid");
    if (! $uid)
        returnMessage(- 1, "您未提交要删除教师的uid");
    $classid = get_post("classid");
    if (! $classid)
        returnMessage(- 2, "您未提交要删除教师的班级id");
    removeTeacherToClass($uid, $classid);
    $back = array();
    $back["op"] = "removeList";
    $back["uid"] = $uid;
    sendNotice($uid, "你被{sender}取消担任 $classid 班的老师，请知悉。", getSessionUid());
    returnMessage(0, "删除成功", $back);
}
if ($op == "add_teacher") {
    $classid = get_post("classid");
    if (! $classid)
        returnMessage(- 1, "你没有输入班级");
    $class = getClass($classid);
    if (! $class)
        returnMessage(- 2, "指定班级未找到");
    $account = get_post("account");
    if (! $account)
        returnMessage(- 3, "你未填写账号");
    $uid = getUID($account);
    if ($uid == - 1)
        returnMessage(- 4, "无法查询到你所寻找的用户");
    $profile = getProfile($uid);
    if (! $profile)
        returnMessage(- 5, "无法查询到此人的档案");
    if ($profile["user_type"] != 2)
        returnMessage(- 6, "你所要添加的用户，不是一个教师，如需指定他为该班级的任课教师，请通过成员编辑选项卡，将其编辑为教师后再进行次操作");
    $classes = getTeacherClasses($uid);
    if ($classes) {
        if (isset($classes[$classid])) {
            returnMessage(- 7, "该教师已经在该班级任教了");
        }
    }
    $subject = getTeacherSubject($uid, false);
    $teachers = getTeachersByClassid($classid);
    if ($teachers) {
        foreach ($teachers as $v) {
            if ($v["sid"] == $subject["sid"])
                returnMessage(- 7, "该班级已经有" . $subject["displayname"] . "科目的任课教师了");
        }
    }
    addTeacherToClass($uid, $classid);
    $back = array();
    $back["card"] = getCardInfo($uid);
    $back["subject"] = $subject["displayname"];
    $back["classid"] = $classid;
    $back["op"] = "addTeacher";
    sendNotice($uid, "你被{sender}设定为 $classid 班的" . $subject["displayname"] . "老师，请知悉。", getSessionUid());
    returnMessage(0, "添加成功", $back);
}
if ($op == "deal_apply_student") {
    checkAdminPermission("admin.class.apply_class", "admin.class.*", true); // 权限判断
    $uid = get_post("uid");
    if ($uid == - 1 || ! $uid)
        returnMessage(- 1, "无法查询到你所寻找的用户");
    if (! getApplyData($uid))
        returnMessage(- 2, "该学生目前没有任何申请");
    $op = get_post("op");
    $note = get_post("note");
    if ($op == "approve")
        approveClassData($uid, getSessionUid(), $note);
    else
        rejectClassData($uid, getSessionUid(), $note);
    returnMessage(0, "该申请成功处理该申请", array(
        "op" => "sent_apply",
        "uid" => $uid
    ));
}
if ($op == "search_class_apply") {
    checkAdminPermission("admin.class.apply_class", "admin.class.*", true); // 权限判断
    $classid = get_post("classid");
    if (! $classid)
        returnMessage(- 1, "你没有输入班级");
    $class = getClass($classid);
    if (! $class)
        returnMessage(- 2, "指定班级未找到");
    returnMessage(0, "操作成功", array(
        "op" => "loadApplyList",
        "data" => getApplyList($classid)
    ));
}
if ($op == "listClasses") {
    checkAdminPermission("admin.class.list_class", "admin.class.*", true); // 权限判断
    returnMessage(0, "操作成功", array(
        "op" => "listAllClasses",
        "data" => listAllClasses()
    ));
}
if ($op == "search_manage_class") {
    checkAdminPermission("admin.class.manage_class", "admin.class.*", true); // 权限判断
    $classid = get_post("classid");
    if (! $classid)
        returnMessage(- 1, "你没有输入班级");
    $class = getClass($classid);
    if (! $class)
        returnMessage(- 2, "指定班级未找到");
    returnMessage(0, "操作成功", array(
        "op" => "manage_data",
        "data" => getStudents($classid)
    ));
}
if ($op == "remove_student") {
    checkAdminPermission("admin.class.manage_class", "admin.class.*", true); // 权限判断
    $uid = get_post("uid");
    if (! $uid)
        returnMessage(- 1, "您未提交要删除学生的uid");
    removeStudent($uid);
    $back = array();
    $back["op"] = "removeStudent";
    $back["uid"] = $uid;
    sendNotice($uid, "你被{sender}取消当前班级的学籍，请知悉，如有异议请联系操作者。", getSessionUid());
    returnMessage(0, "删除成功", $back);
}
?>